package rhinestone.TUI;

import rhinestone.Control.OrderController;
import rhinestone.Model.Order;

/**
 * OrderUI is responsible for order management user interface.
 * It prints the menu and prompts user inputs whenever needed.
 *
 * @author Group 2
 * @version 2010.12.16.
 */
public class OrderUI extends UI {
	private final String ID_MESSAGE = "ID: ";
	private OrderController controller;

	public OrderUI(OrderController controller) {
		this.controller = controller;
	}

	/**
	 * The logic of the UI.
	 */
	public void startMenu() {
		controller.initializeOrder();
		boolean finished = false;
		while(!finished) {	
			printMenu();
			int choice = promptChoiceInput();
			switch(choice) {
				case 1:
					addProductChoice();
					break;
				case 2:
					removeProductChoice();
					break;
				case 3:
					executeOrderChoice();
					finished = true;
					break;
				case 4:
					printOrderChoice();
					break;
				case 5:
					searchOrderChoice();
					break;
				case 6:
					cancelOrderChoice();
					finished = true;
					break;
				default:
					break;
			}
		}
	}

	/**
	 * The menu which the users see when they get here.
	 */
	protected void printMenu() {
		System.out.println(" ----- ORDER MENU -----");
		System.out.println("\t(-1-) Add product");
		System.out.println("\t(-2-) Remove product");
		System.out.println("\t(-3-) Execute order");
		System.out.println("\t(-4-) Print current order");
		System.out.println("\t(-5-) Search for order");
		System.out.println("\t(-6-) Back / Cancel order");
	}

	/**
	 * Down there are a methods which are called by startMenu method
	 * depending on the users choice
	 */
	private void addProductChoice() {
		String barcode = promptStringInput(BARCODE_MESSAGE);
		int amount = promptIntInput(AMOUNT_MESSAGE);
        controller.addProduct(barcode, amount);
	}

	private void removeProductChoice() {
		String barcode = promptStringInput(BARCODE_MESSAGE);
		int amount = promptIntInput(AMOUNT_MESSAGE);
		controller.removeProduct(barcode, amount);
	}

	private void executeOrderChoice() {
        controller.confirmOrder();
	}

	private void printOrderChoice() {
		Order order = controller.getCurrentOrder();
		printOrder(order);
	}

	private void searchOrderChoice() {
		int id = promptIntInput(ID_MESSAGE);
		Order order = controller.getOrder(id);
		if(order != null)
			printOrder(order);
		else
			System.err.println("Order with ID " + id + " does NOT exists.");
	}
	private void cancelOrderChoice() {
		controller.cancelOrder();
	}

	private void printOrder(Order order) {
		System.out.println(order);
	}
}
